{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import JsonOutputParser\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "from langchain_core.pydantic_v1 import BaseModel, Field\n",
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(model=\"gpt-4o\", verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Joke(BaseModel):\n",
    "    setup: str = Field(description=\"提出一个有关笑话的问题\")\n",
    "    punchline: str = Field(description=\"回答这个笑话问题\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "joke_query = \"给我讲一个冷笑话\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将模型输出结果按照定义的Joke对象输出成JSON格式\n",
    "parser = JsonOutputParser(pydantic_object=Joke)\n",
    "\n",
    "# print(parser.get_format_instructions())\n",
    "# 如果没有指定需要解析的对象，默认会输出JSON格式，但JSON格式的字段是不固定的。\n",
    "# parser = JsonOutputParser()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = PromptTemplate(\n",
    "    template=\"请回答用户的问题。\\n{format_instructions}\\n{query}\\n\",\n",
    "    input_variables=[\"query\"],\n",
    "    partial_variables={\"format_instructions\": parser.get_format_instructions()},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = prompt | llm | parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'joke': \"有一天，一只北极熊走进了一家咖啡店，对店员说：'请给我一杯热咖啡……还有一块蛋糕。' 店员问：'为什么你说话有这么长的停顿？' 北极熊回答：'因为我是北极熊啊。'\"}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke({\"query\": joke_query})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{'setup': ''}\n",
      "{'setup': '为什么'}\n",
      "{'setup': '为什么数学'}\n",
      "{'setup': '为什么数学书'}\n",
      "{'setup': '为什么数学书总'}\n",
      "{'setup': '为什么数学书总是'}\n",
      "{'setup': '为什么数学书总是很'}\n",
      "{'setup': '为什么数学书总是很不'}\n",
      "{'setup': '为什么数学书总是很不开心'}\n",
      "{'setup': '为什么数学书总是很不开心？'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': ''}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它里面'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它里面有'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它里面有太'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它里面有太多'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它里面有太多问题'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它里面有太多问题了'}\n",
      "{'setup': '为什么数学书总是很不开心？', 'punchline': '因为它里面有太多问题了。'}\n"
     ]
    }
   ],
   "source": [
    "for chunk in chain.stream({\"query\": joke_query}):\n",
    "    print(chunk)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
